#include "statusbarinformer.h"
#include "utils.h"



StatusBarInformer::StatusBarInformer(QStatusBar *bar, ClientsDataBase *base) :
    flag_emit(false)
{
    this->bar = bar;
    cbase = base;
    connect(&timer, SIGNAL(timeout()), this, SLOT(update()));

    uSpeedi = dSpeedi = tUploadedi = tDownloadedi = 0;

    upSpeed = new QLabel("");
    bar->addWidget(upSpeed);

    dSpeed = new QLabel("");
    bar->addWidget(dSpeed);

    totUpload = new QLabel("");
    bar->addWidget(totUpload);

    totDownload = new QLabel("");
    bar->addWidget(totDownload);

    setUploadSpeedInfo(uSpeedi);
    setDownloadSpeedInfo(dSpeedi);
    setTotalUploadInfo(tUploadedi);
    setTotalDownloadInfo(tDownloadedi);

    startScan();
}



StatusBarInformer::~StatusBarInformer()
{
    stopScan();
    delete upSpeed;
    delete dSpeed;
    delete totUpload;
    delete totDownload;
}



void StatusBarInformer::stopScan()
{
    timer.stop();
}



void StatusBarInformer::startScan()
{
    if(!timer.isActive())
        timer.start(1000);
}



void StatusBarInformer::setUploadSpeedInfo(size_t speed)
{
    QString range = "b/s";

    /* convert b/ms to b/s and calculate */
    double r_size = Utils::speedToHigherDimensions(speed, range);
    upSpeed->setText(QString("upload: %1%2 ").arg(QString::number(r_size, 'f', 2)).arg(range));
}



void StatusBarInformer::setDownloadSpeedInfo(size_t speed)
{
    QString range;
    double r_size = Utils::bytesToHigherDimensions(speed, range);
    dSpeed->setText(QString("download: %1%2 ").arg(QString::number(r_size, 'f', 2)).arg(range));
}



void StatusBarInformer::setTotalUploadInfo(size_t size)
{
    QString range = "kb";
    double r_size = Utils::bytesToHigherDimensions(size, range);

    totUpload->setText(QString("total uploaded: %1%2 ").arg(QString::number(r_size, 'f', 2)).arg(range));
}



void StatusBarInformer::setTotalDownloadInfo(size_t size)
{
    QString range;
    double r_size = Utils::bytesToHigherDimensions(size, range);

    totDownload->setText(QString("total downloaded: %1%2 ").arg(QString::number(r_size, 'f', 2)).arg(range));
}


void StatusBarInformer::addTxRxValue(int tx, int rx)
{
    tUploadedi += tx;
    tDownloadedi += rx;

    informeOfUploading(1);
}


void StatusBarInformer::update()
{
    uSpeedi = dSpeedi = 0;

    cbase->lock();
    size_t sz = cbase->size();
    for(size_t i=0; i<sz; ++i)
    {
        uSpeedi += cbase->at(i)->threadsUploadSpeed(false);
        dSpeedi += cbase->at(i)->threadsDownloadSpeed(false);
    }
    cbase->unlock();

    setUploadSpeedInfo(uSpeedi);
    setDownloadSpeedInfo(dSpeedi);
    setTotalUploadInfo(tUploadedi);
    setTotalDownloadInfo(tDownloadedi);

    informeOfUploading(uSpeedi);
}


void StatusBarInformer::informeOfUploading(quint64 uSpeed)
{
    if(updTime.elapsed() < 500 && !uSpeed) return;
    updTime.restart();

    if(uSpeed > 0 && flag_emit == false)
    {
        flag_emit = true;
        emit uploading();
    }
    else if(uSpeed < 1 && flag_emit == true)
    {
        flag_emit = false;
        emit quiescentState();
    }
}
